﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.User;

public partial class sysadmin_UserManager_AddStudent :PowerCreator.LMS.Web.Base.BasePage
{
    User opUser = new User();
    Department opDepartment = new Department();
    LogicServer.NetMeetingClass objNetMeetingClass;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            BinderDepartmentClass();
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass = new LogicServer.NetMeetingClass();
            }
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle_AddStudent.Text = GetLanageValueByKey("lbl_AddStudent_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Account_loading.Text = GetLanageValueByKey("lbl_AddStudent_Account_loading", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_LoadingName.Text = GetLanageValueByKey("lbl_AddStudent_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_PassWord.Text = GetLanageValueByKey("lbl_AddStudent_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);
        lblUserLoginIP.Text = GetLanageValueByKey("lbl_AddStudent_IsLoginIP", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_BaseInformaition.Text = GetLanageValueByKey("lbl_AddStudent_BaseInformaition", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddStudent_Name.Text = GetLanageValueByKey("lbl_AddStudent_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Number.Text = GetLanageValueByKey("lbl_AddStudent_Number", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Sex.Text = GetLanageValueByKey("lbl_AddStudent_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_IdNumber.Text = GetLanageValueByKey("lbl_AddStudent_IdNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Born.Text = GetLanageValueByKey("lbl_AddStudent_Born", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Mail.Text = GetLanageValueByKey("lbl_AddStudent_Mail", PowerCreator.LMS.Language.LanagetItemType.Page);
      //  lbl_Class.Text = GetLanageValueByKey("lbl_AddStudent_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Nation.Text = GetLanageValueByKey("lbl_AddStudent_Nation", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Phone.Text = GetLanageValueByKey("lbl_AddStudent_Phone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Native.Text = GetLanageValueByKey("lbl_AddStudent_Native", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Post.Text = GetLanageValueByKey("lbl_AddStudent_Post", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_MobileTelephone.Text = GetLanageValueByKey("lbl_AddStudent_MobileTelephone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Address.Text = GetLanageValueByKey("lbl_AddStudent_Address", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Remark.Text = GetLanageValueByKey("lbl_AddStudent_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_addstudent_selClass.Text = GetLanageValueByKey("lbl_addstudent_selClass", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Birth_css.Text = "(格式：1980-01-01)";
        radbtn_Male.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Female.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtUserName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtRealName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
        ftv_txtUserPwd.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_AddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }


    /// <summary>
    /// 检查页面数据
    /// </summary>
    /// <returns></returns>
    private bool CheckPageData()
    {

        if (txt_UserName.Text.Trim().Length < 5)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("Login_username_msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
            txt_UserName.BackColor = System.Drawing.Color.BlanchedAlmond;
            txt_UserName.Focus();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        if (txt_UserPwd.Text.Trim().Length < 6)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("Login_username_msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
            txt_UserPwd.BackColor = System.Drawing.Color.BlanchedAlmond;
            txt_UserPwd.Focus();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        string search = string.Format("UserName = '{0}'", txt_UserName.Text);
        if (opUser.Count(search) != 0)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("theme_Register_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
            txt_UserName.BackColor = System.Drawing.Color.BlanchedAlmond;
            txt_UserName.Focus();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }

        if (!string.IsNullOrEmpty(txt_Birth.Text))
        {
            if (!TimeUtils.IsDateTime(txt_Birth.Text.Trim()))
            {
                Script.Alert(txt_Birth.Text.Trim());
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Birth.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Birth.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }       
        if (!string.IsNullOrEmpty(txt_Email.Text))
        {
            if (!PageValidate.IsEmail(txt_Email.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Email.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Email.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        
        //if (!string.IsNullOrEmpty(txt_Tel.Text))
        //{
        //    if (!PageValidate.IsPhone(txt_Phone.Text))
        //    {
        //        lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
        //        txt_Tel.Focus();
        //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        //        return false;
        //    }
        //}
        if (!string.IsNullOrEmpty(txt_Phone.Text))
        {
            if (!PageValidate.IsNumber(txt_Phone.Text))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g5", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Phone.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Phone.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }

        //if (!string.IsNullOrEmpty(txt_Tel.Text))
        //{
        //    if (!PageValidate.IsNumber(txt_Tel.Text))
        //    {
        //        lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g6", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
        //        txt_Tel.Focus();
        //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        //        return false;
        //    }
        //}
        if (ddl_Class.Items.Count == 0)
        {
            lbl_addstudent_selectdepartment.Text = GetLanageValueByKey("theme_Register_Msg4",PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }

    //private void BinderDepartment()
    //{
    //    string search = "DepartmentType=1";
    //    DataTable dt = opDepartment.Query(search);
    //    ArrayList list = opDepartment.GetDepartmentTree(dt);
    //    ddl_Department.DataSource = list;
    //    ddl_Department.DataTextField = "Text";
    //    ddl_Department.DataValueField = "Value";
    //    ddl_Department.DataBind();
    //}
    private void BinderDepartmentClass()
    {
        //if(ddl_Department.SelectedValue)
       // VOTDepartment vo = opDepartment.Load(int.Parse(ddl_Department.SelectedValue.ToString()));
        //string search = "DepartmentType=2 and DepartmentFatherId=" + vo.DepartmentId.ToString();
        string search = "DepartmentType=2 ";
        DataTable dt = opDepartment.Query(search);
        ddl_Class.DataSource = dt;
        ddl_Class.DataTextField = "DepartmentName";
        ddl_Class.DataValueField = "DepartmentId";
        ddl_Class.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;

        VOTUser voUser = new VOTUser();
        VOTStudent voStudent = new VOTStudent();

        voUser.UserName = txt_UserName.Text;
        voUser.UserPassword = txt_UserPwd.Text;
        

        voStudent.StudentAddress = txt_Address.Text;
        if(!string.IsNullOrEmpty( txt_Birth.Text))
            voStudent.StudentBirthday = DateTime.Parse(txt_Birth.Text);

        if(!string.IsNullOrEmpty(ddl_Class.SelectedValue.ToString()))
            voStudent.FKStudentDepartmentId = int.Parse(ddl_Class.SelectedValue.ToString());

        voStudent.StudentCode = txt_StudentCode.Text;
        voStudent.StudentDescription = txt_Description.Text;
        voStudent.StudentEmail = txt_Email.Text;
        voStudent.StudentHomeplace = txt_Homeplace.Text;
        voStudent.StudentIdentityCard = txt_Idcard.Text;
        voStudent.StudentMobileTelephone = txt_Phone.Text;
        voStudent.StudentName = txt_RealName.Text;
        voStudent.StudentNation = txt_Nation.Text;
        voStudent.StudentPost = txt_Post.Text;
        voStudent.StudentSex = 0;
        voUser.UserType = 0;
        voUser.UserIscheck = 0;
        voUser.UserLoginIP = txt_UserloginIP.Text;

        if (radbtn_Female.Checked)
            voStudent.StudentSex = 1;

        voStudent.StudentTelephone = txt_Tel.Text;
        int UserID=0;
        if (opUser.AddStudent(voUser, voStudent, GetCreatedByInfo(),ref UserID))
        {

            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass.WebAddUser(UserID, voUser.UserName, voUser.UserPassword, (ulong)voUser.UserRule, voUser.UserImageIndex, voStudent.StudentDescription, voStudent.StudentSex, voStudent.StudentTelephone, voUser.UserType);
            }
            string url = "StudentManager.aspx";
            Script.RedirectURL(url);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "StudentManager.aspx";
        Script.RedirectURL(url);
    }
    protected void ddl_Department_SelectedIndexChanged(object sender, EventArgs e)
    {
      //  BinderDepartmentClass();
    }
}
